package reflect;

import reflect.annotations.AutoRunClass;

import java.io.File;
import java.net.URISyntaxException;

/**
 输出与当前类Tset4在同一个包中所有类是否被@AutoRunClass标注
 */
public class Test4 {
    public static void main(String[] args) throws Exception {
        //通过Test3类定位它所在目录
        File dir = new File(
                Test4.class.getResource(".").toURI()
        );
        String packagename = Test4.class.getPackage().getName();
        //获取该目录中所有.class文件
        File[] subs = dir.listFiles(f -> f.getName().endsWith(".class"));
        for(File sub : subs) {
            String filename = sub.getName();
            String classname = filename.substring(0, filename.indexOf("."));
            Class cls = Class.forName(packagename + "." + classname);
            if(cls.isAnnotationPresent(AutoRunClass.class)) {
                System.out.println(cls + "被标注了!");
            }else {
                System.out.println(cls + "未被标注!");
            }
        }
    }

}
